package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Vector;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.user.client.ui.Label;

public class AddGroupPopupPanel extends DialogBox implements Serializable {
	private static final long serialVersionUID = -492370192313538632L;
	Vector<Group> groupVector = new Vector<Group>();
	Vector<Group> addedGroupVector = new Vector<Group>();
	
	Group selectedViewGroup;
	Group selectedAddGroup;
	
	ListBox viewGroupsListbox;
	ListBox addedGroupsListbox;
	Button addButton;
	Button removeButton;
	Button addGroupsButton;
	Button cancelButton;
	
	/**
	 * @wbp.parser.constructor
	 */
	public AddGroupPopupPanel(Vector<Group> _groupVector) {
		this.setText("Add Groups");
		this.setGlassEnabled(true);
		groupVector = _groupVector;
		this.setGlassEnabled(true);
		
		setSize("604px", "361px");
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		setWidget(absolutePanel);
		absolutePanel.setSize("592px", "419px");
		
		viewGroupsListbox = new ListBox();
		viewGroupsListbox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (viewGroupsListbox.getSelectedIndex() != -1) {
					selectedViewGroup = groupVector.get(viewGroupsListbox.getSelectedIndex());
				}
			}
		});
		absolutePanel.add(viewGroupsListbox, 10, 34);
		viewGroupsListbox.setSize("215px", "282px");
		viewGroupsListbox.setVisibleItemCount(5);
		
		if (groupVector != null && groupVector.capacity() > 0) {
			for (Group group : groupVector) {
				if (group == null)
					continue;
				
				viewGroupsListbox.addItem(group.getName());
			}
		}		
		
		addedGroupsListbox = new ListBox();
		addedGroupsListbox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (addedGroupsListbox.getSelectedIndex() != -1) {
					selectedAddGroup = addedGroupVector.get(addedGroupsListbox.getSelectedIndex());
				}
			}
		});
		addedGroupsListbox.setVisibleItemCount(5);
		absolutePanel.add(addedGroupsListbox, 367, 34);
		addedGroupsListbox.setSize("215px", "282px");
		
		addButton = new Button("Add >");
		addButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedViewGroup != null && viewGroupsListbox.getSelectedIndex() != -1) {
					int selectedIndex = viewGroupsListbox.getSelectedIndex();
					addedGroupsListbox.addItem(selectedViewGroup.getName());
					addedGroupVector.add(selectedViewGroup);
					viewGroupsListbox.removeItem(viewGroupsListbox.getSelectedIndex());
					groupVector.remove(selectedViewGroup);
					viewGroupsListbox.setSelectedIndex(selectedIndex);
					
					try {
						viewGroupsListbox.setItemSelected(selectedIndex, true);
					}
					catch (Exception e) {}
					
					if (viewGroupsListbox.getSelectedIndex() != -1) {
						selectedViewGroup = groupVector.get(viewGroupsListbox.getSelectedIndex());
					}
					else {
						selectedViewGroup = null;
					}
				}
			}
		});
		absolutePanel.add(addButton, 10, 322);
		
		removeButton = new Button("< Remove");
		removeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedAddGroup != null && addedGroupsListbox.getSelectedIndex() != -1) {
					int selectedIndex = addedGroupsListbox.getSelectedIndex();
					viewGroupsListbox.addItem(selectedAddGroup.getName());
					groupVector.add(selectedAddGroup);
					addedGroupsListbox.removeItem(addedGroupsListbox.getSelectedIndex());
					addedGroupVector.remove(selectedAddGroup);
					addedGroupsListbox.setSelectedIndex(selectedIndex);
					
					try {
						addedGroupsListbox.setItemSelected(selectedIndex, true);
					}
					catch (Exception e) {}
					
					if (addedGroupsListbox.getSelectedIndex() != -1) {
						selectedAddGroup = addedGroupVector.get(addedGroupsListbox.getSelectedIndex());
					}
					else {
						selectedAddGroup = null;
					}
				}
			}
		});
		absolutePanel.add(removeButton, 367, 322);
		removeButton.setSize("86px", "24px");
		
		addGroupsButton = new Button("Add Groups");
		absolutePanel.add(addGroupsButton, 370, 381);
		addGroupsButton.setSize("103px", "28px");
		
		cancelButton = new Button("Cancel");
		absolutePanel.add(cancelButton, 479, 381);
		cancelButton.setSize("103px", "28px");
		
		Label lblGroups = new Label("Available Groups:");
		absolutePanel.add(lblGroups, 10, 10);
		
		Label lblAddedGroups = new Label("Added Groups:");
		absolutePanel.add(lblAddedGroups, 367, 10);
		lblAddedGroups.setSize("103px", "18px");
	}

	public AddGroupPopupPanel() {		

	}
}
